/*
 * Copyright (c) Eldanichu  DMR.js (2024-9-12).
 */
import {
	RequestAPI
} from '@/utils/RequestAPI'
import {
	StoreUtils
} from '../utils/StoreUtils'
import {
	defineStore
} from 'pinia'
import {
	StringUtils
} from '../utils/StringUtils'
import {
	ParamUtil
} from '../utils/convertable/ParamUtil'

export const useStockStore = defineStore('StockService', {
	state() {
		return {}
	},
	actions: {
		async getStorageList(payload = {}) {
			let api = new RequestAPI()
			const {
				API_TYPE
			} = payload || {}
			const _payload = new ParamUtil(payload)
				.excludeKey(['API_TYPE']).build()
			const urls = {
				'in': {
					url: 'storage/in',
					method: 'get',
					params: {
						..._payload,
					}
				},
				'otherin': {
					url: 'storage/in',
					method: 'get',
					params: {
						..._payload
					}
				},
				'out': {
					url: 'storage/out',
					method: 'get',
					params: {
						..._payload,
					}
				},
				'otherout': {
					url: 'storage/out',
					method: 'get',
					params: {
						..._payload,
					}
				}
			}

			const {
				url,
				method,
				params
			} = urls[API_TYPE]

			try {
				const res = await api.request({
					url: StoreUtils.parseUrl(url),
					method,
					params: {
						...StoreUtils.toCamelParams(params)
					}
				})

				const {
					data,
					msg,
					code
				} = res

				const {
					Rows = [], Total = 0
				} = data || {}

				return {
					Rows,
					Total,
					msg,
					code
				}
			} catch (e) {
				throw new Error(e)
			}
		},
		async getDetail(payload) {
			let api = new RequestAPI()
			const {
				API_TYPE
			} = payload || {}
			const _payload = new ParamUtil(payload)
				.excludeKey(['API_TYPE']).build()
			const urls = {
				'in': {
					url: 'storage/in',
					method: 'get',
					params: {
						..._payload
					}
				},
				'out': {
					url: 'storage/out',
					method: 'get',
					params: {
						..._payload
					}
				},
			}

			const {
				url,
				method,
				params
			} = urls[API_TYPE]
			const {
				id
			} = _payload
			try {
				const res = await api.request({
					url: StoreUtils.parseUrl(url, id),
					method,
					params: {
						...params
					}
				})
				const {
					data,
					msg
				} = res
				return {
					data,
					msg
				}
			} catch (e) {
				throw new Error(e)
			}
		},
		async getSyncProductIn(payload) {
			let api = new RequestAPI()
			try {
				const res = await api.request({
					url: StoreUtils.parseUrl('/homepage/productInWh'),
					method: "get",
				})
				const {
					data,
					msg
				} = res
				return {
					data,
					msg
				}
			} catch (e) {
				throw new Error(e)
			}
		},
		async listMod(payload) {
			let api = new RequestAPI()
			const {
				API_TYPE
			} = payload || {}
			const urls = {
				"in": {
					url: "storage/in",
					method: "post",
					params: {
						...payload
					}
				},
				"out": {
					url: "storage/out",
					method: "post",
					params: {
						...payload
					}
				},
				"otherin": {
					url: "storage/in",
					method: "post",
					params: {
						...payload
					}
				},
				"otherout": {
					url: "storage/out",
					method: "post",
					params: {
						...payload
					}
				}
			}

			const {
				url,
				method,
				params
			} = urls[API_TYPE]
			const {
				form
			} = params || {}
			try {
				const res = await api.request({
					url: StoreUtils.parseUrl(url),
					method,
					data: {
						...new ParamUtil(form)
						.excludeKey([
							"API_TYPE",
							"__PAGE_ID",
							"PAGE_TYPE_NAME",
							"BUSS_TYPE"
						]).build()
					}
				})
				const {
					data,
					msg,
					code,
					success
				} = res

				return {
					msg,
					code,
					success
				}
			} catch (e) {
				throw new Error(e)
			}
		},
		async stockListDelete(payload) {
			let api = new RequestAPI()
			const {
				API_TYPE,
				ids = []
			} = payload || {}
			const urls = {
				"in": {
					url: "storage/in",
					method: "delete",
					params: {
						...payload
					}
				},
				"out": {
					url: "storage/out/remove",
					method: "get",
					params: {
						...payload
					}
				},
				"otherin": {
					url: "storage/in",
					method: "delete",
					params: {
						...payload
					}
				},
				"otherout": {
					url: "storage/out/remove",
					method: "get",
					params: {
						...payload
					}
				}
			}

			const {
				url,
				method
			} = urls[API_TYPE]

			const ids_str = ids.join(",")
			try {
				const res = await api.request({
					url: StoreUtils.parseUrl(url, ids_str),
					method
				})
				const {
					msg,
					code,
					success
				} = res

				return {
					msg,
					code,
					success
				}
			} catch (e) {
				throw new Error(e)
			}
		},
		async listDelete(payload) {
			let api = new RequestAPI()
			const {
				ids = []
			} = payload || {}
			const ids_str = ids.join(',')
			try {
				const res = await api.request({
					url: StoreUtils.parseUrl('/ordergoods/delte', ids_str),
					method: 'get'
				})
				const {
					msg,
					code
				} = res

				return {
					msg,
					code
				}
			} catch (e) {
				throw new Error(e)
			}
		},



	}
})